package IHM;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import base.Base;
import battlestein.BattleWar;

/**
 * 
 * @author LocoRocoBattleStein
 *
 */

	public class GameDisplay extends JPanel implements MouseListener,MouseMotionListener{
	
		
		private BattleWar battleWar;
		
		private static final long serialVersionUID = 1L;
		
		private int rayon;
		private int rayonMax;
		private int clickX;
		private int clickY;
		private int enteredX;
		private int enteredY;
		private int startBaseX;
		private int startBaseY;
		private Base startBase;
		private int targetBaseX;
		private int targetBaseY;
		private Base targetBase;
		private int towerX;
		private int towerY;
		
		private boolean isClicked;
		private boolean isAgainClicked;
		private boolean cancel;
		private boolean baseSelected;
		private boolean targetBaseSelected;
		private boolean entered;
		
		

		private boolean targetConfirmed;
		private boolean drawLine;

		
		private boolean towerSelected;
		private boolean menuTowerSelected;
		
		private Image menuTowerImg;
		private long lastUpdate ;
		private int counter;
		
		
		/**
		 * Constructor GameDisplay
		 * @param bw
		 */	
	public GameDisplay(BattleWar bw){
		this.addMouseMotionListener(this);
		this.addMouseListener(this);
		this.battleWar = bw;
		baseSelected = false;
		isClicked = false;
		setCancel(true);
		rayon = 0;
		lastUpdate = System.currentTimeMillis();
		counter = 0; 
	}
	/**
	 * 	
	 */
	public void paint(Graphics g){
		

		battleWar.draw(g);
		g.setColor(Color.black);
		
 		if(menuTowerSelected && entered){
	        g.drawImage(menuTowerImg, enteredX-30/2, enteredY-35/2,30,35, null);
		}
 		
		if(baseSelected && !targetBaseSelected ){

					g.drawLine(startBaseX, startBaseY, enteredX , enteredY);
				
		}
		if(targetBaseSelected){
			targetConfirmed = true;
			targetBaseSelected = false;
		}
		if (targetConfirmed){
			long current = System.currentTimeMillis();
			if((current - lastUpdate) > 100) {
				lastUpdate = current;
				if(drawLine){
					g.setColor(Color.red);
					g.drawLine(startBaseX, startBaseY, targetBaseX , targetBaseY);
					drawLine = false;
					counter++;
				}
				else{ 
					drawLine = true;
				}
				if(counter ==4){
					counter = 0;
					targetConfirmed = false;
				}
			}
		}
		
		if(towerSelected){
			g.setColor(new Color(255,0,0,80));
			g.fillOval(towerX-rayon/2, towerY-rayon/2, rayon, rayon);
			if(rayon<rayonMax) rayon+=20;
		}	
	} 

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub	
		entered = true;
			
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if ( SwingUtilities.isLeftMouseButton (arg0) ) {
			setMouseXY(arg0.getX(), arg0.getY());
			System.out.println("X : "+clickX+"---- Y : "+clickY);
			rayon =0;	
			isClicked = true;

			if(baseSelected){
				isAgainClicked = true;
			}
			
		}
		if ( SwingUtilities.isRightMouseButton (arg0) ) {
			setCancel(true);
		}
		
	}
	
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		entered = false;
		
	}

	public void mouseClicked(MouseEvent arg0) {

		// TODO Auto-generated method stub
		
	}
	
	

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		setMouseEnteredXY(arg0.getX(), arg0.getY());
		// TODO Auto-generated method stub
		
	}

	
	public boolean getTargetBaseSelected() {
		return targetBaseSelected;
	}

	public void setTargetBaseSelected(boolean targetBaseSelected) {
		this.targetBaseSelected = targetBaseSelected;
	}

	
	public boolean getIsAgainClicked() {
		return isAgainClicked;
	}

	public void setIsAgainClicked(boolean isAgainClicked) {
		this.isAgainClicked = isAgainClicked;
	}

	public boolean getTowerSelected() {
		return towerSelected;
	}

	public void setTowerSelected(boolean towerSelected) {
		this.towerSelected = towerSelected;
	}

	public boolean getIsClicked() {
		return isClicked;
	}

	public void setIsClicked(boolean isTowerSelected) {
		this.isClicked = isTowerSelected;
	}

	
	public boolean getBaseSelected() {
		return baseSelected;
	}

	public void setBaseSelected(boolean baseSelected) {
		this.baseSelected = baseSelected;
	}

	public int getStartBaseX() {
		return startBaseX;
	}

	public void setStartBaseX(int startBaseX) {
		this.startBaseX = startBaseX;
	}

	public int getStartBaseY() {
		return startBaseY;
	}

	public void setStartBaseY(int startBaseY) {
		this.startBaseY = startBaseY;
	}

	public Base getStartBase() {
		return startBase;
	}

	public void setStartBase(Base startBase) {
		this.startBase = startBase;
	}

	public int getTargetBaseX() {
		return targetBaseX;
	}

	public void setTargetBaseX(int targetBaseX) {
		this.targetBaseX = targetBaseX;
	}

	public int getTargetBaseY() {
		return targetBaseY;
	}

	public void setTargetBaseY(int targetBaseY) {
		this.targetBaseY = targetBaseY;
	}

	public Base getTargetBase() {
		return targetBase;
	}

	public void setTargetBase(Base targetBase) {
		this.targetBase = targetBase;
	}

	public int getTowerX() {
		return towerX;
	}

	public void setTowerX(int towerX) {
		this.towerX = towerX;
	}

	public int getTowerY() {
		return towerY;
	}

	public void setTowerY(int towerY) {
		this.towerY = towerY;
	}

	public void setMouseXY(int x,int y){
		clickX=x;
		clickY=y;
	}
	public void setMouseEnteredXY(int x, int y){
		enteredX=x;
		enteredY=y;
	}
	
	public int getClickX() {
		return clickX;
	}

	public void setClickX(int clickX) {
		this.clickX = clickX;
	}

	public int getClickY() {
		return clickY;
	}

	public void setClickY(int clickY) {
		this.clickY = clickY;	
	}
	
	public void setRayonMax(int r) {
		this.rayonMax = r;
	}

	public int getRayonMax() {
		return rayonMax;
	}
	
	public void setCancel(boolean cancel) {
		this.cancel = cancel;
	}

	public boolean getCancel() {
		return cancel;
	}
	
	public boolean getMenuTowerSelected() {
		return menuTowerSelected;
	}

	public void setMenuTowerSelected(boolean menuTowerSelected) {
		this.menuTowerSelected = menuTowerSelected;
	}

	public Image getMenuTowerImg() {
		return menuTowerImg;
	}

	public void setMenuTowerImg(Image menuTowerImg) {
		this.menuTowerImg = menuTowerImg;
	}
	



	
}
